package com.tianguxx.utill;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;

import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Xml;

import com.tianguxx.bean.Article;

public class PullParseUtill
{

	public static Map<String, String> getApk(InputStream inputStream) throws Exception
	{
		Map<String, String> map = null;
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inputStream, "UTF-8");
		int event = parser.getEventType();
		while (event != XmlPullParser.END_DOCUMENT)
		{
			switch (event)
			{
			case XmlPullParser.START_DOCUMENT:// ÅÐ¶Ïµ±Ç°ÊÂŒþÊÇ·ñÊÇÎÄµµ¿ªÊŒÊÂŒþ
				map = new HashMap<String, String>();// ³õÊŒ»¯map
				break;

			case XmlPullParser.START_TAG:
				if ("na".equals(parser.getName()))
				{
					map.put("na", parser.nextText());
				}
				if ("d".equals(parser.getName()))
				{
					map.put("d", parser.nextText());
				}
				break;
			}
			event = parser.next();// œøÈëÏÂÒ»žöÔªËØ²¢Ž¥·¢ÏàÓŠÊÂŒþ

		}
		return map;

	}

	public static Map<String, String> getVersion(InputStream inputStream) throws Exception
	{

		Map<String, String> map = null;
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inputStream, "UTF-8");
		int event = parser.getEventType();
		while (event != XmlPullParser.END_DOCUMENT)
		{
			switch (event)
			{
			case XmlPullParser.START_DOCUMENT:// ÅÐ¶Ïµ±Ç°ÊÂŒþÊÇ·ñÊÇÎÄµµ¿ªÊŒÊÂŒþ
				map = new HashMap<String, String>();// ³õÊŒ»¯map
				break;

			case XmlPullParser.START_TAG:
				if ("v".equals(parser.getName()))
				{
					map.put("v", parser.nextText());
				}
				break;
			}
			event = parser.next();// œøÈëÏÂÒ»žöÔªËØ²¢Ž¥·¢ÏàÓŠÊÂŒþ

		}
		return map;
	}

	public static Map<String, String> getConfig(InputStream inputStream) throws Exception
	{

		Map<String, String> map = null;
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inputStream, "UTF-8");
		int event = parser.getEventType();
		while (event != XmlPullParser.END_DOCUMENT)
		{
			switch (event)
			{
			case XmlPullParser.START_DOCUMENT:// ÅÐ¶Ïµ±Ç°ÊÂŒþÊÇ·ñÊÇÎÄµµ¿ªÊŒÊÂŒþ
				map = new HashMap<String, String>();// ³õÊŒ»¯map
				break;

			case XmlPullParser.START_TAG:
				if ("s".equals(parser.getName()))
				{
					map.put("s", parser.nextText());
				}
				if ("rt".equals(parser.getName()))
				{
					map.put("rt", parser.nextText());
				}
				if ("st".equals(parser.getName()))
				{
					map.put("st", parser.nextText());
				}
				if ("ru".equals(parser.getName()))
				{
					map.put("ru", parser.nextText());
				}
				if ("v".equals(parser.getName()))
				{
					map.put("v", parser.nextText());
				}
				break;
			}
			event = parser.next();// œøÈëÏÂÒ»žöÔªËØ²¢Ž¥·¢ÏàÓŠÊÂŒþ

		}
		return map;
	}

	public static Map<String, String> getPhoneNumber(InputStream inputStream) throws Exception
	{

		Map<String, String> map = null;
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inputStream, "UTF-8");
		int event = parser.getEventType();
		while (event != XmlPullParser.END_DOCUMENT)
		{
			switch (event)
			{
			case XmlPullParser.START_DOCUMENT:// ÅÐ¶Ïµ±Ç°ÊÂŒþÊÇ·ñÊÇÎÄµµ¿ªÊŒÊÂŒþ
				map = new HashMap<String, String>();// ³õÊŒ»¯map
				break;

			case XmlPullParser.START_TAG:
				if ("config".equals(parser.getName()))
				{
					map.put("mobile", parser.getAttributeValue(0));
				}
				break;
			}
			event = parser.next();// œøÈëÏÂÒ»žöÔªËØ²¢Ž¥·¢ÏàÓŠÊÂŒþ
		}
		return map;
	}

	public static List<Article> getArticles(InputStream inputStream) throws Exception
	{

		List<Article> articles = null;
		Article article = null;
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inputStream, "UTF-8");
		int event = parser.getEventType();
		while (event != XmlPullParser.END_DOCUMENT)
		{
			switch (event)
			{
			case XmlPullParser.START_DOCUMENT:
				articles = new ArrayList<Article>();
				break;

			case XmlPullParser.START_TAG:

				if ("article".equals(parser.getName()))
				{
					article = new Article();
					article.setId(Integer.parseInt(parser.getAttributeValue(0)));
				}
				if (article != null)
				{
					if ("title".equals(parser.getName()))
					{
						article.setTitle(parser.nextText());
					}
					else if ("matter".equals(parser.getName()))
					{
						article.setMatter((parser.nextText()));
					}
				}

				break;
			case XmlPullParser.END_TAG:
				if ("article".equals(parser.getName()))
				{
					articles.add(article);
					article = null;
				}
				break;
			}
			event = parser.next();
		}
		return articles;

	}

}
